<?php
class IndexController extends AppController {
    public function beforeFilter() {
        $this->Auth->allow();
    }
    public function isAuthorized($user) {
        $this->loadModel('User');
        $this->User->id = $user['id'];
        $this->User->unbindModel(array('hasMany' => array('Test')));
        $role = $this->User->read('role');
        if($role['User']['role'] == 'none') {
            if(in_array($this->action, array('index', 'search'))) {
                return true;
            }
        }
        if($role['User']['role'] == 'admin') {
            if(in_array($this->action, array('index', 'search'))) {
                return true;
            }
        }
    }
    public function index() {
        $this->set('title_for_layout', 'Thư Viện Đề Thi - Kiểm Tra :: ChomoTech');
        $this->loadModel('Subject');
        $subject = $this->Subject->find('all');
        $this->loadModel('Test');
        $this->Test->unbindModel(array('hasMany' => array('Question', 'Like'), 'belongsTo' => array('Subject', 'Grade', 'Training')));
        $i = 0;
        foreach($subject as $sj) {
            $subject[$i]['Subject']['count'] = $this->Test->find('count', array('conditions' => array('subject_id' => $sj['Subject']['id'], 'share' => 1)));
            $i++;
        }
        $this->set('subjectView', $subject);
    }
    public function search() {
        $this->set('title_for_layout', 'Tìm Kiếm :: ChomoTech');
    }
}
?>